/**
 * 
 */
package com.training1.core.web.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.BOXUserEntity;
import com.training1.core.entity.ContractBaseEntity;
import com.training1.core.entity.ContractCrudEntity;
import com.training1.core.entity.UnderCompanyEntity;
import com.training1.core.modules.service.ContractService;

/**
 * @author yunlei
 * 登录主页
 *	合同管理（增删改查）
 *	合同逾期
 */
@Controller
@RequestMapping("contract")
public class ContractController extends BaseController{
	
	@Resource
	private ContractService contractService;
	
	@RequestMapping("loadMainPage")
	public String loadMainPage(Model model){
		BOXUserEntity user = contractService.findUserId();
		Map<String,Object> map = contractService.findCategList(user.getId());
		//判断用户权限(参数:功能id,登录用户id)
		if(promit(0,user.getId())){
			//有权限的话再进行此类操作
			model.addAttribute("result", map);
			return "contract/loadMainPage";
		}else{
			//没有权限,直接跳转到提示页面
			return "contract/notLimitsPage";
		}
	}
	
	@RequestMapping("search")
	public String searchContract(Model model){
		BOXUserEntity user = contractService.findUserId();
		Map<String,Object> map = contractService.findCategList(user.getId());
		//判断用户权限(参数:功能id,登录用户id)
		if(promit(0,user.getId())){
			//有权限的话再进行此类操作
			model.addAttribute("result", map);
			return "contractCrud/searchContract";
		}else{
			//没有权限,直接跳转到提示页面
			return "contract/notLimitsPage";
		}
	}
	
	@RequestMapping(value="/yqhtPage",method=RequestMethod.GET)
	public String yqhtPage(String flag){
		if("u9".equals(flag)){
			return "contract/yqhtPage";
		}else if("u21".equals(flag)){
			return "contract/yqhtPage";
		}else if("u33".equals(flag)){
			return "contract/yqhtPage";
		}else if("u45".equals(flag)){
			return "contract/yqhtPage";
		}else if("u57".equals(flag)){
			return "contract/yqhtPage";
		}
		return "contract/yqhtPage";

	}
	
	@RequestMapping(value="/yqhtList",method=RequestMethod.GET)
	@ResponseBody
	public Page<ContractBaseEntity> yqhtList(String customerPhone,String customerName,String company,String recom,Page<ContractBaseEntity> page){
		BOXUserEntity user = contractService.findUserId();
		page.getSearch().put("id", user.getId());
		page.getSearch().put("customerPhone", customerPhone);
		page.getSearch().put("customerName", customerName);
		page.getSearch().put("company", company);
		page.getSearch().put("recom", recom);
		contractService.findList(page.getSearch());
		return page;
	}
	
	@RequestMapping(value="/companyCombox",method=RequestMethod.GET)
	@ResponseBody
	public List<UnderCompanyEntity> companyCombox(){
		List<UnderCompanyEntity> companyItems = new ArrayList<>();
		UnderCompanyEntity companyEntity = new UnderCompanyEntity();
		companyEntity.setOrgName("全部");
		companyEntity.setId(null);
		companyItems.add(companyEntity);
		List<UnderCompanyEntity> items = contractService.findCompanyCombox();
		companyItems.addAll(items);
		return companyItems;
	}
	
	//导出
	@RequestMapping("export")
	@ResponseBody
	public void export(HttpServletResponse resp,String customerPhone,String customerName,String company,String recom,Page<ContractBaseEntity> page){
		BOXUserEntity user = contractService.findUserId();
		page.getSearch().put("id", user.getId());
		page.getSearch().put("customerPhone", customerPhone);
		page.getSearch().put("customerName", customerName);
		page.getSearch().put("company", company);
		page.getSearch().put("recom", recom);
		contractService.export(resp, page.getSearch());
	}
	@RequestMapping("edit")
	public String edit(Integer id,Model model){
		model.addAttribute("id", id);
		return "contractCrud/contractEdit";
	}
	
	@RequestMapping("deletes")//删除合同页面跳转
	public String deleteContract(Integer id,Model model){
		model.addAttribute("id", id);
		return "contractCrud/deleteContract";
	}
	@RequestMapping("delContract")//删除合同
	public String delContract(Integer id,Model model){
		contractService.delContract(id);
		return "redirect:/contract/search";
	}
	
	
	//更新获取数据的
	@RequestMapping("get")
	@ResponseBody
	public List<ContractCrudEntity> getContract(Integer id){
		return contractService.getContract(id);
	}
	@RequestMapping("add")
	public String add(){
		return "contractCrud/contractAdd";
	}
	/**
	 * 添加的保存方法
	 * @param entity
	 * @param lvyueContract	//履约合同图片名(entity)
	 * @param qitaContract	//其他附件图片名(entity)
	 * @param minusSpn	//履约合同的附件名(entity)
	 * @param qtfjName	//其他附件的附件名(entity)
	 * @return 返回到main页面
	 */
	@RequestMapping("save")
	public String saveContract(ContractCrudEntity entity){
		contractService.saveContract(entity);
		return "redirect:/contract/search";
	}
	
	/**
	 * 修改的保存方法
	 * @param entity
	 * @param lvyueContract	//履约合同图片名(entity)
	 * @param qitaContract	//其他附件图片名(entity)
	 * @param minusSpn	//履约合同的附件名(entity)
	 * @param qtfjName	//其他附件的附件名(entity)
	 * @return 返回到main页面
	 */
	@RequestMapping("save1")
	public String updateSaveContract(ContractCrudEntity entity){
		contractService.updateSaveContract(entity);
		return "redirect:/contract/search";
	}
	/**
	 * 查询所有的合同
	 * @param customerPhone
	 * @param customerName
	 * @param status
	 * @param endFlag
	 * @param company
	 * @param recom
	 * @param page
	 * @return
	 */
	@RequestMapping("searchContractList")
	@ResponseBody
	public Page<ContractBaseEntity>  searchContractList(
			String customerPhone,String customerName,
				Integer status,String endFlag,String company,
						String recom,Page<ContractBaseEntity> page){
		BOXUserEntity user = contractService.findUserId();
		page.getSearch().put("id", user.getId());
		page.getSearch().put("customerPhone", customerPhone);
		page.getSearch().put("customerName", customerName);

			page.getSearch().put("status", status);
		
		
		if("true".equals(endFlag)){
			page.getSearch().put("endFlag", endFlag);
		}else{
			page.getSearch().put("endFlag", null);
		}
		page.getSearch().put("company", company);
		page.getSearch().put("recom", recom);
		contractService.searchContractList(page.getSearch());
		return page;
	}
	@RequestMapping("exportList")
	@ResponseBody
	public Page<ContractBaseEntity>  exportList(HttpServletResponse resp,
			String customerPhone,String customerName,
				Integer status,String endFlag,String company,
						String recom,Page<ContractBaseEntity> page){
		BOXUserEntity user = contractService.findUserId();
		page.getSearch().put("id", user.getId());
		page.getSearch().put("customerPhone", customerPhone);
		page.getSearch().put("customerName", customerName);

			page.getSearch().put("status", status);
		if("true".equals(endFlag)){
			page.getSearch().put("endFlag", endFlag);
		}else{
			page.getSearch().put("endFlag", null);
		}
		page.getSearch().put("company", company);
		page.getSearch().put("recom", recom);
		contractService.exportList(resp,page.getSearch());
		return page;
	}
	
	@RequestMapping("exist")
	@ResponseBody
	public Integer exist(Integer id){
		return contractService.exist(id);
		//select count(*) from T_CONTRACT_DELAY where IS_VALID=0 and CONTRACTBASEID=1		
	}
	
	//判断权限的私有方法
	private Boolean promit(Integer funId,Integer userId){
		//判断用户权限(参数:功能id,登录用户id)
		if(contractService.getUserRole(funId,userId) != 0 
				&& contractService.getUserRole(funId,userId) !=null){
			return true;
		}else
			return false;
	}
}
